package com.xnfh.equipment.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.EquipmentGl
import com.xnfh.common.bean.EquipmentGlBean
import com.xnfh.equipment.R
import com.xnfh.equipment.adapter.EquipmentGlAdapter
import com.xnfh.equipment.databinding.ActivityInternetThingsLayoutBinding
import com.xnfh.equipment.viewmodel.InternetThingsViewModel
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver

/**
 * @Description:物联网设备
 * @Author: ziheng
 * @CreateDate: 2023/12/27 18:31
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/27 18:31
 * @UpdateRemark: 更新说明
 */
class InternetThingsActivity: BaseActivity<ActivityInternetThingsLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_internet_things_layout
    companion object{
        fun start(context: Context, bundle: Bundle? = null){
            val intent = Intent(context, InternetThingsActivity::class.java)
            bundle?.let { intent.putExtra("bundle",bundle) }
            context.startActivity(intent)
        }
    }
    private var adapter: EquipmentGlAdapter? = null
    private var page = 1
    private var viewModel: InternetThingsViewModel? = null

    override fun init(savedInstanceState: Bundle?) {
        setHeaderTitle(getString(R.string.str_wlwsb))
        mBind.internetThingsRecycle.layoutManager = LinearLayoutManager(this)
        adapter = EquipmentGlAdapter()
        mBind.internetThingsRecycle.adapter = adapter
        mBind.internetThingsRefresh.autoRefresh()
        mBind.internetThingsRefresh.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel?.requestWeatherList(page,mBind.internetThingsSearch.text.toString())
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel?.requestWeatherList(page,mBind.internetThingsSearch.text.toString())
            }

        })
        mBind.internetThingsSearch.setOnDeleteClickListener{
            mBind.internetThingsRefresh.autoRefresh()
        }
        //键盘的搜索按钮
        mBind.internetThingsSearch.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mBind.internetThingsRefresh.autoRefresh()
                val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(v.windowToken, 0) //强制隐藏键盘
                return@OnEditorActionListener true
            }
            false
        })

        adapter?.setOnItemClickListener { adapter, view, position ->
            val bundle = Bundle()
            bundle.putString("id",(adapter.data[position] as EquipmentGlBean).gatewayId)
            bundle.putString("type",(adapter.data[position] as EquipmentGlBean).gatewayModel)
            bundle.putString("name",(adapter.data[position] as EquipmentGlBean).gatewayName)
            WeatherMessageActivity.start(this,bundle)
        }
    }

    override fun observe() {
        viewModel = InternetThingsViewModel()
        viewModel?.weatherList?.observe(this,object :BaseStateObserver<EquipmentGl>(){
            override fun getRespDataSuccess(it: EquipmentGl) {
                super.getRespDataSuccess(it)
                mBind.internetThingsRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if(it.total <= 0){
                    adapter?.setNewInstance(null)
                    adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1){
                    adapter?.setNewInstance(it.records.toMutableList())
                }else{
                    adapter?.addData(it.records)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.internetThingsRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
    }
}